package com.controls.ws;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.content.Context;

import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.events.WebServiceCommunicatorListener;
import com.objects.Contact;
import com.objects.ContactCheck;
import com.objects.SecurePackageLog;

public class SecurePackageLogWSControl extends BaseWSControl {
	public SecurePackageLogWSControl(Context cxt,
			WebServiceCommunicatorListener lsn) {
		super(cxt, lsn);
	}

	public boolean getSecure(String accessToken,long contactID) {
		String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>"
				+ "<GetSentSecurePackagesReport xmlns=\"https://www.privacydatasolutions.net/\">"
				+ "<Token>"+accessToken+"</Token>" + "<ContactID>"+contactID+"</ContactID>"
				+ "</GetSentSecurePackagesReport>" + "</soap:Body>"
				+ "</soap:Envelope>";

		fetch(WebServiceFlag.GET_SECURE, content);
		return true;
	}

	
	/**
	 * 
	 * @param xml
	 * @return
	 */

	public ArrayList<SecurePackageLog> parseSecure(String xml) {
		ArrayList<SecurePackageLog> items = new ArrayList<SecurePackageLog>();
		Document doc = getDomElement(xml);
		if (doc == null) {
			return items;
		}

		NodeList results = doc.getElementsByTagName("GetSentSecurePackagesReportResult");
		JSONArray mArray = null;
		if (results.getLength() > 0) {
			Node node = results.item(0);
			try {
				mArray = new JSONArray(node.getTextContent());
			} catch (DOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (mArray != null) {
			for (int i = 0; i < mArray.length(); i++) {
				try {
					JSONObject obj = mArray.getJSONObject(i);
					SecurePackageLog item = new SecurePackageLog();
					if (obj.has("DateTime")) {
						item.setDate(obj.getString("DateTime"));
					}
					if (obj.has("Description")) {
						item.setDescription(obj.getString("Description"));
					}
					if (obj.has("Status")) {
						item.setStatus(obj.getString("Status"));
					}
					items.add(item);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
		return items;
	}

	
}
